﻿#include "HttpServer.h"

HttpServer *m_HttpServer = NULL;

void HttpAcceptedCallback(const QPointer< JQHttpServer::Session > &session){

//    qDebug() <<  session->requestSourceIp();
//    qDebug() <<  session->requestMethod();
//    qDebug() <<  session->requestUrl();
//    qDebug() <<  session->requestCrlf();
//    qDebug() <<  session->requestHeader();
//    qDebug() <<  session->requestBody();
//    qDebug() <<  session->requestUrlPath();
//    qDebug() <<  session->requestUrlPathSplitToList();
//    qDebug() <<  session->requestUrlQuery();
//    qDebug() <<  session->replyHttpCode();
//    qDebug() <<  session->replyBodySize();

    QString method = session->requestMethod();
    QString url = session->requestUrlPath();

    //下发协议
    if(url.contains("Download")){
        QString clientIp;

        QPointer<QIODevice> ioDevice = session->ioDevice();
        QTcpSocket *sock = qobject_cast<QTcpSocket*>(ioDevice);
        if(sock){
            QString ipstr = sock->peerAddress().toString();
            QStringList ips = ipstr.split(":");
            if(ips.size() > 0){
                clientIp = ips.at(ips.size() - 1);//ip最后一组
            }
        }

        QByteArray data = session->requestBody();
        QString downXml = QTextCodec::codecForName("UTF-8")->toUnicode(data);

        session->replyText("ok");

    }else if(url.contains("api")){
        m_HttpServer->webApi->WebHandler(session);

    }else if(url.contains("web")){
        QString url = QString(":%1").arg(session->requestUrlPath());
        //QString url = MyHelper::GetFilePath(session->requestUrlPath());

        //QString url = "E:/Workspaces/QtCreator/TSReceiver/TSRIESystem/res/" + session->requestUrlPath();
        if(url.contains(".png") | url.contains(".jpg") | url.contains(".gif")){
            session->replyImage(url);
        }else{
            session->replyFile(url);
        }

    }else if(url.contains("file")){
        QString url = session->requestUrlPath();
        QString filePath = m_HttpServer->globalData->config->system->filePath + url.replace("/file", "");
        session->replyFile(filePath);
    }else{
        //session->replyText("<script>location.href = '/web/login.html';</script>");
        session->replyText("<script>location.href = '/web/screen.html';</script>");
//        session->replyText("<script>"
//                               "var self=window.open('/web/screen.html','monitor','resizable=1');"
//                               "self.moveTo(0,0);"
//                               "self.resizeTo(screen.availWidth,screen.availHeight);"
//                           "</script>");
    }
}


HttpServer::HttpServer(QObject *parent) : MyObject(parent)
{
    listen = false;
    httpServerManage = NULL;

    webApi = new WebApi();
}

void HttpServer::Start(){
    m_HttpServer = this;

    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
    timer->start(60*1000);
    this->OnTimer();
}

void HttpServer::Stop(){
    httpServerManage->deleteLater();
    delete httpServerManage;
    emit Finished();
}

void HttpServer::OnTimer(){
    if(httpServerManage == NULL){
        int port = this->globalData->config->system->httpPort;
        httpServerManage = new JQHttpServer::TcpServerManage(30);
        httpServerManage->setHttpAcceptedCallback(HttpAcceptedCallback);
        listen = httpServerManage->listen(QHostAddress::Any, port);//设置监听
        MyLog::Info(QString("HttpServer->listen() %1 %2").arg(port).arg(listen));
    }
    if(httpServerManage != NULL && !listen){
        httpServerManage->deleteLater();
        httpServerManage = NULL;
    }
}
